var app = getApp();
var tools = require('../tools/tools.js');
Page({

  data: {
    start : 0,
    end : 9,
    movieArr:[]
    
  },


  onLoad: function (options) {
    var info = options.info;
      console.log(info);
        this.setData({
          info
        });
        wx.showLoading({
          title: '加载中',
        });

      
        switch(info){
          case '正在热映':
            this.getDataMovieList(app.IN_TAHEATERSURL)
            break;
          case '其它电影':
            this.getDataMovieList(app.COMING_SONGURL);
            break;
          case 'TOP250':
            this.getDataMovieList(app.TOP250URL);
            break;
            default:
              break;

        }
  },
  onShow:function(){
   
    wx.setNavigationBarTitle({
      title: this.data.info,
    })
  
  },
  getDataMovieList:function(url){
    var self = this;
      wx.request({
        url: url,
        method:'get',
        header:{
          "Content-Type":'JSON'
        },
        success:function(data){
           var data = data.data.subjects;

           if(data.length> 0){
            setTimeout(function () {
              wx.hideLoading();
              self.renderListMovie(data);
            }, 2000);
           }
        },
        fail:function(e){
          console.log(e);
        }
      })
  },
 renderListMovie:function(data){
 
         this.setData({
          
          resdata:data,
          dataLen:data.length,
         
         });
      
        
      
          // || this.data.start-9 <=this.data.dataLen
        if(this.data.dataLen >= 9 ){
            this.setData({
              movieArr: this.data.movieArr.concat(this.data.resdata.splice(this.data.start,this.data.end))
            })
    
        }else{
          this.setData({
            movieArr:this.data.movieArr.concat(this.data.resdata.splice(0))
          })
           
        }
        this.setData({
          start : this.data.start+this.data.end,
          end : this.data.end,
          dataLen:this.data.resdata.length,
          resdata:this.data.resdata

        });
 },
 onscrolltolower :function(){
    console.log(this.data.dataLen);
   if( this.data.dataLen>=0){
    console.log("到底部");
    this.renderListMovie(this.data.resdata);
   }else{
     
   }

 },
 onToMovieDetail:function(e){
  var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/movieDetail/movie_detail?id=${id}`
      })

},

  

  
})